package com.reflex.method;

import java.io.FileInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * @Author: lizehui
 * @Description: 反射的四个主要类
 * @Date: 2022/11/18/13:53
 */
public class MethodBaseUse {

    public static void main(String[] args) throws Exception {
        //读取配置文件
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/reflex.properties"));

        //类路径字符串
        String classFullPath = properties.get("classFullPath").toString();
        //方法名字符串
        String methodName = properties.get("method").toString();
        //属性字符串
        String attribute = properties.get("attribute").toString();

        //反射的四个主要类
        //1、类
        Class cls = Class.forName(classFullPath);
        Object o = cls.newInstance();
        //2、方法
        Method method = cls.getMethod(methodName);
        //3、属性
        //getField只能获取到public的属性，private不行
        Field field = cls.getField(attribute);
        //4、构造器
        //无参构造器
        Constructor constructor = cls.getConstructor();
        //有参构造器
        Constructor constructor1 = cls.getConstructor(String.class);


        //调用方法
        method.invoke(o);
        //调用属性
        System.out.println(field.get(o));
        //无参构造器
        System.out.println(constructor);
        //有参构造器
        System.out.println(constructor1);


    }


}
